//
//  ForgetCodeViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/17.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "ForgetCodeViewController.h"

#import "ChangeCodeViewController.h"

#import "CManager.h"

#import "NetworkManger+User.h"

#import "LYCGetVerificationCodeButton.h"



@interface ForgetCodeViewController ()<LYCGetVerificationCodeButtonDelegate>



@end

@implementation ForgetCodeViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
}

-(void)viewDidLoad{
        [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.93];
    
    self.title = @"忘记密码";
    
    
    
    UIImage *image = [UIImage imageNamed:@"返回"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(0, 0, 40, 40);
    
    [button setImage:image forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(dimiss:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = item;
    
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.view.frame)/12 + 20, 70, 50)];
    
    label.numberOfLines = 2;
    
    label.text = @"手机号";
    
    label.font = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:label];
    //
    UITextField *registerPhoneNumber = [[UITextField alloc]init];
    
    registerPhoneNumber.frame = CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 30, 240, 40);
    
    registerPhoneNumber.placeholder = @"请输入您的手机号码";
    
    registerPhoneNumber.backgroundColor = [UIColor whiteColor];
    
    registerPhoneNumber.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
    registerPhoneNumber.leftViewMode = UITextFieldViewModeAlways;
    
    [registerPhoneNumber.layer setBorderWidth:2];
    [registerPhoneNumber.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:0.84].CGColor];
    
    registerPhoneNumber.tag = 200;
    
    [self.view addSubview:registerPhoneNumber];
    //
    UITextField *number = [[UITextField alloc]init];
    
    number.frame = CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 140, 240, 40);
    
    number.placeholder = @"请输入验证码";
    
    number.backgroundColor = [UIColor whiteColor];
    
    number.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
    number.leftViewMode = UITextFieldViewModeAlways;
    
    [number.layer setBorderWidth:2];
    [number.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:0.84].CGColor];
    
    number.tag = 202;
    
    [self.view addSubview:number];
    
//    UIButton *surebutton = [UIButton buttonWithType:UIButtonTypeSystem];
//    //
//    surebutton.frame = CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 80, 100, 40);
//    [surebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
//    surebutton.backgroundColor = [UIColor colorWithRed:1.0 green:0.502 blue:0.0 alpha:0.92];
//    
//    surebutton.tintColor = [UIColor whiteColor];
//    
//    [surebutton.layer setBorderWidth:1];
//    [surebutton.layer setBorderColor:[UIColor blackColor].CGColor];
//    [surebutton addTarget:self action:@selector(getsecuritycode1:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:surebutton];
    LYCGetVerificationCodeButton *surebutton = [[LYCGetVerificationCodeButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 80, 100, 40)];
    surebutton.delegate =self;
    [self.view addSubview:surebutton];

    
    



    
    UILabel *label1  = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.view.frame)/12 + 140 , 70, 50)];
    
    label1.numberOfLines = 2;
    
    label1.text = @"验证码";
    
    label1.font = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:label1];
    
    UIButton *finshbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    finshbutton.frame = CGRectMake(30, CGRectGetMaxY(self.view.frame) - 140, 315, 40);
    [finshbutton setTitle:@"下一步" forState:UIControlStateNormal];
    finshbutton.backgroundColor = [UIColor colorWithRed:1.0 green:0.502 blue:0.0 alpha:0.92];
    
    finshbutton.tintColor = [UIColor whiteColor];
    
    [finshbutton.layer setBorderWidth:1];
    
    [finshbutton.layer setBorderColor:[UIColor blackColor].CGColor];
    
    [finshbutton addTarget:self action:@selector(nextcode1:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:finshbutton];

    
}




-(void)dimiss:(UIButton *)sender{
    
    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)nextcode1:(UIButton *)sender{
    
    UITextField *phoneNumber = (UITextField *)[self.view viewWithTag:200];
    UITextField *testingNumber = (UITextField *)[self.view viewWithTag:202];
    
    
    
    
    
    if (phoneNumber.text.length != 11) {
        [self otherpopuppromptBox:@"请输入正确的手机号"];
    }else if (phoneNumber.text.length == 11 && testingNumber.text.length != 6){
        
        [self otherpopuppromptBox:@"请输入正确格式的验证码"];
        
    }else if (phoneNumber.text.length == 11 && testingNumber.text.length == 6){
        
        [NetworkManger forgetCodeThenDetectionWithPhoneNumber:phoneNumber.text code:testingNumber.text successHander:^(id responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"status"] isEqualToString:@"0"]) {
              
                                
                ChangeCodeViewController *CHVC = [[ChangeCodeViewController alloc]init];
                
                CHVC.block = ^(){
                    return phoneNumber.text;
                };
                
                
                [self.navigationController pushViewController:CHVC animated:YES];

            }else if ([responseObject[@"status"] isEqualToString:@"-1"]){
                
                [self otherpopuppromptBox:@"验证码输入错误"];
               
            }
           
        } failureHander:^(NSError *error) {
            
            [self otherpopuppromptBox:@"验证失败"];
            
        }];
    }

    
   
    
    
    
}
-(void)doNetAction{
    
    UITextField *phoneNumber = (UITextField *)[self.view viewWithTag:200];
    
    if ([CManager validateMobile:phoneNumber.text]) {
        
        [NetworkManger verifPhoneNumberWithPhoneNumber:phoneNumber.text successHandler:^(id responseObject) {
            if ([responseObject[@"status"] isEqualToString:@"0"]) {
                [NetworkManger getDetectionNumberWhenGetingLostCodeWithPhoneNumber:phoneNumber.text successHander:^(id responseObject) {
                    [self otherpopuppromptBox:@"获取成功"];
                } failureHander:^(NSError *error) {
                    
                }];

            }else if ([responseObject[@"status"] isEqualToString:@"-1"]){
                [self otherpopuppromptBox:@"已注册"];
            }else if ([responseObject[@"status"] isEqualToString:@"1"]){
                [self otherpopuppromptBox:@"账号被禁用"];
            }
            
        } failureHandler:^(NSError *error) {
            
        }];
}else{
        [self otherpopuppromptBox:@"请输入正确的手机号"];
    }
    
    
    
    
    
    
}
#pragma mark -- worning
-(void)otherpopuppromptBox:(NSString *)string{
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}
@end
